<?php

namespace App\Console\Commands\Mybank;

use App\Models\Common\ConfigModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\System\SystemNotifyService;
use Carbon\Carbon;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class NoOpenWalletNotifyByProvince extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'myBank:no-open-wallet-notify-by-province {--provinceCode=310000}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '未开通网商支付省份通知';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @throws GuzzleException
     */
    public function handle()
    {
        $deadline     = (string)ConfigModel::query()->where('module', 'myBank')->where('name', 'pushNotifyDeadline')->value('value');
        $provinceCode = $this->option('provinceCode');
        if (time() < Carbon::createFromDate($deadline)->endOfDay()->timestamp) {

            $notifyId = (int)ConfigModel::query()
                ->where('module', 'mybank')
                ->where('name', 'openPayNotifyId')
                ->value('value');

            if (!$notifyId) {
                return;
            }

            $userIds = RmsUserRemote::getNoPowerWalletByProvince($provinceCode);
            $userIdArr = array_chunk($userIds, 10);

            foreach ($userIdArr as $userList) {
                Log::info('发送开通网商支付提醒' . json_encode($userList));
                //发送推送
                SystemNotifyService::preSend($notifyId, $userList);
            }

        }

    }
}
